<script setup>
import FirstTitle from "@comp/FirstTitle";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { onMounted } from "vue";
import { searchDataRyfh } from "@/api/search";
import { addOrUpdateRyfh } from "@/api/addOrUpdate";

const city = window.globalObj.name;
const editorRef = shallowRef();

const toolbarConfig = {};
const editorConfig = {
  placeholder: "请输入内容...",
};
const mode = ref("default");

// 加载html
const initHtml = () => {
  searchDataRyfh({ m: "相关保障-政治工作要点" }).then((res) => {
    const { data } = res;
    valueHtml.value = data.find((x) => x.k === "政治工作要点")?.v || "";
  });
};

const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例，重要！
};

const valueHtml = ref(
  "保障任务：坚决执行人防指挥部的意图和指示，按照围绕中心、贴近实战、鼓舞斗志的原则，开展宣传教育、思想政治工作，动员大家认清形势，统一思想和行动，强化共同御敌的决心，树立敢打必胜的信心，从政治上、组织上、思想上、行动上保证夺取防空袭斗争的胜利。<br/>保障部署：由政治工作部牵头负责，战时按照人防指挥部统一部署制定政治工作保障方案计划。临战前，进行全面应急动员；空袭中，进行抗敌宣传动员，必要时组织心理筛查、心理疏导、心理干预、心理救治等行动；消除空袭后果时，进行抢险抢修动员。<br/>保障方法：广泛开展思想动员发动、宣传鼓动、干部调配、奖励处罚、优抚安抚、反心理战和人民防空知识教育等工作，统筹安排宣传报道等事宜。"
);

const saveProgramme = () => {
  ElMessageBox.confirm(`确定要保存吗?`, "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    customClass: "message-box",
  }).then(() => {
    addOrUpdateRyfh({
      m: "相关保障-政治工作要点",
      data: [
        {
          k: "政治工作要点",
          v: valueHtml.value,
        },
      ],
    })
      .then(() => {
        ElMessage.success("操作成功");
      })
      .catch(() => {});
  });
};

onMounted(() => {
  initHtml();
});
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (!editor) return;
  editor.destroy();
});
</script>
<template>
  <div class="">
    <FirstTitle name="政治工作要点" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef"
        :defaultConfig="toolbarConfig"
        :mode="mode"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml"
        :defaultConfig="editorConfig"
        :mode="mode"
        @onCreated="handleCreated"
      />
    </div>

    <div class="flex-justify-end top-20">
      <el-button type="primary" class="save-btn" @click="saveProgramme"
        >保存</el-button
      >
    </div>
  </div>
</template>
<style scoped lang="scss"></style>